global path "/gpfs/home/psg24/GGP_Brokers/"

tempfile temp
local exp_measure all_experience_l1
*set scheme sonia_new
local file cont`exp_measure'_pgp
local save_file "$path/code/matlab/save_calibration`file'.xlsx"


while 1 < 0 {
preserve
cap log close
log using calibration_static, replace

gen endog_exp_pool = exp(endog_exp) - 1
replace endog_exp_pool = 101 if endog_exp_pool > 100 & !missing(endog_exp_pool)
replace endog_exp_pool = round(endog_exp_pool)

gen endog_exp_pool_medium = endog_exp_pool * (period == 2)
gen endog_exp_pool_bust = endog_exp_pool * (period == 1)
gen endog_exp_pool_boom = endog_exp_pool * (period == 3)

* set up the output matrix
tab endog_exp_pool, matrow(exp_groups)
matrix exp_groups = exp_groups'	 

scalar num_groups = colsof(exp_groups)

local log_close_price_censrow 5
local log_list_price_orig_censrow 3
local sale365row 4
local dom_ggp_censrow 6
foreach x of varlist log_list_price_orig_cens log_close_price_cens sale365 dom_ggp_cens {
	sum `x' if endog_exp_pool_medium == 0 & period == 2, meanonly
	local `x'_medium =r(mean)
	mat U_medium = J(1,num_groups,1)*``x'_medium'
	sum `x' if endog_exp_pool_bust == 0 & period == 1, meanonly
	local `x'_bust =r(mean)
	mat U_bust = J(1,num_groups,1)*``x'_bust'
	sum `x' if endog_exp_pool_boom == 0 & period == 3, meanonly
	local `x'_boom =r(mean)
	mat U_boom = J(1,num_groups,1)*``x'_boom'
	reghdfe `x' i.endog_exp_pool_boom i.endog_exp_pool_bust  i.endog_exp_pool_medium   $controls if year > 2001, absorb(zip_mo) cluster(mls_id)
	matrix b=e(b)
	scalar num_c = colsof(b)
 	mat coeff_boom = b[1,1..num_groups]+U_boom
	mat coeff_bust = b[1,num_groups+1..2*num_groups]+U_bust
	mat coeff_medium = b[1,2*num_groups+1..3*num_groups]+U_medium


	foreach agr_st in boom bust medium {
    	putexcel set `save_file', modify sheet(`agr_st')	 		
    	putexcel A1 = matrix(exp_groups)
		putexcel A``x'row' = matrix(coeff_`agr_st')

    	putexcel A10 = ``x'_`agr_st''
		}
	}

gen temp_sale_price = exp(log_close_price_cens)
// Exporting Average Sale Price by State
foreach agr_st in boom bust medium {
putexcel set `save_file', modify sheet(`agr_st')	 		
sum temp_sale_price if `agr_st' == 1
putexcel A14 = `r(mean)'
}
drop temp_sale_price	

restore
}

*************************************************
******** using the panel data *******************
**** this part uses laged aggregate states*******
*************************************************
/*** THIS NEEDS TO BE RUN SEPARATELY, AFTER main ANALYSIS CODE IS DONE ****/


*while 1 < 0 {
use $path/data/buyerseller_brokerpanel.dta, clear
quietly do $path/code/analysis/broker_panel_prelim.do
egen max_experience = max(all_experience_l1), by(agent_id mls_id)
drop if max_experience > 200
drop max_experience
merge m:1 year using "$path/data/StateTransitionExercise/year_states_Ch12"
tab _merge
keep if _merge==3
drop _merge
drop t1* t2* t3*
sort agent year

drop boom bust recovery period
gen bust = state-10*floor(state/10)==1 
gen medium = state-10*floor(state/10)==2
gen boom = state-10*floor(state/10)==3
capture label drop periods
label define periods 1 "Boom" 2 "Bust" 3 "Medium"
gen period = 1*boom + 2 * bust + 3*medium
label values period periods

//  Learning by experience. For each experience level, the change in experience is unconditional.
tsset agent year
gen delta_exp_stay = num_bs - `exp_measure' if active == 1

foreach agr_st in boom bust medium {
preserve		
keep if  `agr_st' == 1
collapse delta_exp_stay, by(`exp_measure')
mkmat delta_exp_stay
matrix delta_exp_stay = delta_exp_stay'
putexcel set `save_file', modify sheet(`agr_st')
putexcel A8 = matrix(delta_exp_stay)
restore
}


// experience advantage
sum `exp_measure' if active==1,d
areg num_bs `exp_measure' i.period i.period#c.`exp_measure' if `exp_measure'<`r(p99)' & active==1, absorb(main_fips)
putexcel set `save_file', modify sheet(distribution)
putexcel A12 = _b[`exp_measure'] 


// entry rates by bust/med/boom
cap drop i
gen i=1
bysort period: egen num_agents = total(i)
bysort period: egen num_entr = total(entrant)
gen entry_rate = num_entr/num_agents
bysort period: replace entry_rate = . if _n>1
foreach agr_st in boom bust medium {
	sum entry_rate if `agr_st'==1
	putexcel set `save_file', modify sheet(`agr_st')
	putexcel A13 = `r(mean)' // entry rate
}

** generate entry rates by state as well
cap drop i
gen i=1
drop num_agents num_entr entry_rate
bysort state: egen num_agents = total(i)
bysort state: egen num_entr = total(entrant)
gen entry_rate = num_entr/num_agents
levelsof(state), local(agr_st_vals)
local count=1
foreach agr_st in `agr_st_vals' {
	sum entry_rate if state==`agr_st'
	putexcel set `save_file', modify sheet(states)
	putexcel B`count' = `r(mean)' // entry rate	
	putexcel A`count' = `agr_st'
	local count = `count'+1
}

** num listings ***
bysort year: egen tot_list = total(num_list)
bysort year: replace tot_list = . if _n>1
 foreach agr_st in boom bust medium {
	sum tot_list if `agr_st'==1
	putexcel set `save_file', modify sheet(`agr_st')
	putexcel A11 = `r(mean)'
}


// exit rates 
tempfile temp
save `temp'
collapse exit boom bust medium, by(`exp_measure' period)
foreach agr_st in boom bust medium {
	preserve
	keep if `agr_st'==1
	mkmat exit
	matrix exit = exit'
	putexcel set `save_file', modify sheet(`agr_st')
	putexcel A7 = matrix(exit)
	restore
}

** generate exit rates by state as well
use `temp', clear
collapse exit, by(`exp_measure' state)
levelsof(state) if ~missing(exit), local(agr_st_vals)
local count = 1
foreach agr_st in `agr_st_vals' {
	preserve
	keep if state==`agr_st'
	mkmat exit
	matrix exit = exit'
	putexcel set `save_file', modify sheet(states_exit)
	putexcel B`count' = matrix(exit)
	putexcel A`count' = `agr_st' 
    local count=`count'+1
	restore
}

*** Save Distribution ***************************
use `temp', clear
cap drop i
drop if `exp_measure'>100
gen i=1
preserve
collapse (sum) i if active==1,by(period `exp_measure') 
drop if `exp_measure'>100
mkmat period `exp_measure' i, matrix(period_exp_num)
putexcel set `save_file', modify sheet(distribution)
putexcel A1 = matrix(period_exp_num)

restore
preserve
collapse (sum) i if active==1,by(state `exp_measure') 
drop if `exp_measure'>100
sort state `exp_measure'
mkmat state `exp_measure' i, matrix(period_exp_num)
putexcel set `save_file', modify sheet(distribution_state)
putexcel A1 = matrix(period_exp_num)

restore

*}

distinct agent if active == 1
preserve
collapse (count) i = agent if active == 1, by(year)
sum i

